package com.leviassaf.androidbasics;

import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SharedPrefs extends Activity implements OnClickListener {
	
	EditText edt;
	TextView txtvwSharedPrefText;
	final String STR_FILE_NAME = "MySharedString";
	SharedPreferences sharedPreferences;
	int intKey = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharedpreferences);
		setJavaRefs();
		sharedPreferences = getSharedPreferences(STR_FILE_NAME, MODE_PRIVATE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);	

		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		Button btnSave = (Button)findViewById(R.id.btnSave);
		Button btnLoad = (Button)findViewById(R.id.btnLoad);
		Button btnLoadAllSharedPrefs = (Button)findViewById(R.id.btnLoadAllSharedPrefs);
		Button btnClearSharedPrefs = (Button)findViewById(R.id.btnClearSharedPrefs);
		edt = (EditText)findViewById(R.id.edtSharedText);
		txtvwSharedPrefText = (TextView)findViewById(R.id.txtDataResults);
		btnSave.setOnClickListener(this);
		btnLoad.setOnClickListener(this);
		btnLoadAllSharedPrefs.setOnClickListener(this);
		btnClearSharedPrefs.setOnClickListener(this);
	}
	
	public int getHighestKey(){
		//get highest integer key in shared preferences
	    int intHighestKey = -1;
		Object[] arrObjKeys = sharedPreferences.getAll().keySet().toArray();
		if (arrObjKeys.length != 0){
		    intHighestKey = Integer.parseInt(arrObjKeys[0].toString());  
		    for(int i = 1; i < arrObjKeys.length; i++){  
		        if(Integer.parseInt(arrObjKeys[i].toString()) > intHighestKey){  
		            intHighestKey = Integer.parseInt(arrObjKeys[i].toString());  
		        }  
		    }  
		}
	    return intHighestKey;  
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		SharedPreferences.Editor editor;
		sharedPreferences = getSharedPreferences(STR_FILE_NAME, MODE_PRIVATE);
		int intMaxKey = getHighestKey();
		int intKey;
		Object[] arrObjValues = sharedPreferences.getAll().values().toArray();
		switch (v.getId()){
		case R.id.btnSave:
			//store user input in new entry of shared preferences
			String strData = edt.getText().toString();
			//96
			editor = sharedPreferences.edit();
			if (intMaxKey == -1){
				intKey = 0;
			}
			else{
				intKey = intMaxKey + 1;
			}
			editor.putString(Integer.toString(intKey), strData);
			editor.commit();
			edt.setText("");
			edt.requestFocus();
			break;
		case R.id.btnLoad:
			String strSharedData = sharedPreferences.getString(Integer.toString(intMaxKey), "No value has been saved yet as a shared preference");
			txtvwSharedPrefText.setText(strSharedData);
			txtvwSharedPrefText.setVisibility(View.VISIBLE);
			break;
		case R.id.btnLoadAllSharedPrefs:
			String strAllSharedPrefsValues = "";
			for (int i = arrObjValues.length - 1; i >= 0; i--){
				strAllSharedPrefsValues += arrObjValues[i].toString() + "\n";
			}
			txtvwSharedPrefText.setText(strAllSharedPrefsValues);
			txtvwSharedPrefText.setVisibility(View.VISIBLE);
			break;
		case R.id.btnClearSharedPrefs:
			editor = sharedPreferences.edit();
			editor.clear();
			editor.commit();
			txtvwSharedPrefText.setVisibility(View.INVISIBLE);
			break;
		}
	}
}
